package com.king.run.util;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;


import com.king.run.base.MyApplication;

import java.io.File;

/**
 * 作者：shuizi_wade on 2017/7/13 11:18
 * 邮箱：674618016@qq.com
 */
public class Utils {
    /**
     * 获取状态栏高度,在页面还没有显示出来之前
     *
     * @param a
     * @return
     */
    public static int getStateBarHeight(Activity a) {
        int result = 0;
        if (null == a) {
            return result;
        }
        int resourceId = a.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = a.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 强制隐藏输入法键盘
     */
    public static void hideInput(Context context, View view){
        InputMethodManager inputMethodManager =
                (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    /**
     * 缓存路径
     *
     * @return
     */
    public static String getCacheFilePath() {
        File cacheDir = MyApplication.getContext().getExternalCacheDir();
        try {
            if (cacheDir == null) {
                String sdCard = Environment.getExternalStorageDirectory()
                        .getPath();
                if (StringUtil.isNotBlank(sdCard)) {
                    cacheDir = new File(sdCard + "/Android/data/"
                            + MyApplication.getContext().getPackageName());
                }
            }
            if (cacheDir == null) {
                cacheDir = MyApplication.getContext().getFilesDir();
            }

            if (!cacheDir.exists()) {
                cacheDir.mkdirs();
            }
        } catch (Exception e) {
        }
        return cacheDir.getPath();
    }
}
